//
//  statusCellOriginaView.swift
//  NSinaWeibo
//
//  Created by LoliQueen on 16/2/20.
//  Copyright © 2016年 LoliQueen. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class statusCellOriginaView: UIView {
    
    //约束属性
    var bottomConstraint : Constraint?
    ///绑定模型数据，给子控件赋值
    var status : Status? {
        didSet{
            iconImageView.sd_setImageWithURL(status?.user?.profileUrl)
            userNameLabel.text = status?.user?.name
            //后期完善
            //            timeLabel.text = status?.created_at
            //            sourceLabel.text = status?.source
            mbrankImageView.image = status?.user?.mbrankImage
            verified_typeImageView.image = status?.user?.verifiedImage
            textLabel.text = status?.text
            
            ///给配置视图的 图片地址数组赋值
            pictureView.pictureUrls = status?.pictureUrls
            
            ///根据视图地址，判断底部约束参考
            //status?.imageURLs 永远都不为空 如果没有配图视图会给定一个空数组
            //where : 可以获取到前面快速赋值不为空的对象
            //如果imageURLs 不为空 并且 imageURLs.count 不为0
            //报警：因为添加了多天约束
            
            //移除老约束
            self.bottomConstraint?.uninstall()
            if let imageUrls = status?.pictureUrls  where imageUrls.count != 0 {
                ///有图片
                self.snp_makeConstraints { (make) -> Void in
                   self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(homeVCMargin).constraint
                }
            }else{
                self.snp_makeConstraints { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(homeVCMargin).constraint
                }
            
            }
            
        }
    }
    
    //设置子控件的方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.yellowColor()
        
        setUpUI()   
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        addSubview(iconImageView)
        addSubview(mbrankImageView)
        
        addSubview(userNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        addSubview(verified_typeImageView)
        addSubview(textLabel)
        
        //添加分割线视图
        addSubview(sepView)
        self.sepView.backgroundColor  = UIColor.purpleColor()
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            
            make.height.equalTo(5)
        }
        
        //设置子控件位置
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(homeVCMargin)
            make.top.equalTo(sepView.snp_top).offset(homeVCMargin)
            //约束头像大小
            make.size.equalTo(CGSizeMake(40, 40))
        }
        
        userNameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(homeVCMargin)
            make.top.equalTo(iconImageView.snp_top)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(homeVCMargin)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(homeVCMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(userNameLabel.snp_right).offset(homeVCMargin)
            make.top.equalTo(userNameLabel.snp_top)
        }
        
        verified_typeImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }

        textLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-homeVCMargin)
            make.top.equalTo(iconImageView.snp_bottom).offset(homeVCMargin)
        }
        ///添加图片视图
        addSubview(pictureView)
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(homeVCMargin)
            make.left.equalTo(self.snp_left).offset(homeVCMargin)
            ///设置测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //设置视图自动计算行高
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(homeVCMargin).constraint
        }
        
        
        ///设置头像为原型
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true
        
        
        
    }
    
    
    
    //头像
    lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户名称
    lazy var userNameLabel : UILabel = UILabel(title:"1116IOS" , textSize: 16, textColor: themeColor)
    //微博时间
    lazy var timeLabel :UILabel = UILabel(title: "10:10", textSize: 14, textColor: UIColor.lightGrayColor())
    //微博来源
    lazy var sourceLabel :UILabel = UILabel(title: "隔壁老王", textSize: 14, textColor: UIColor.lightGrayColor())
    //微博等级
    lazy var mbrankImageView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //用户认证类型
    lazy var verified_typeImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    
    
    //微博内容
    lazy var textLabel :UILabel = UILabel(title: "下雨和巧克力更配", textSize: 14, textColor: UIColor.darkGrayColor(), aligmen: NSTextAlignment.Left)
    //配图视图
    lazy var pictureView : statusPictureView = statusPictureView()
    
    //分割线视图
    lazy var sepView : UIView = UIView()
    
    
   

}
